package org.wattdepot.cli.commands;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.wattdepot.cli.CommandLineInterface;

/**
 * List of the commands for the CommandLineInterface.
 * 
 * @author Paul Galiza
 * @author David Joel Lazaro
 * 
 */
public class UserCommands {

  CommandLineInterface cli;

  /**
   * Initializes CommandLineInterface with the client from. CommandLineInterface
   * 
   * @param client The client from CommandLineInterface
   */
  public UserCommands(CommandLineInterface client) {
    this.cli = client;
  }

  /**
   * Starts prompting the user for correct commands.
   */
  public void startPrompt() {

    Scanner scan = new Scanner(System.in);

    boolean promptLoop = true;

    while (promptLoop) {
      System.out.print(">");
      String input = scan.nextLine();

      // Splits the user input into a list of command arguments.
      List<String> commands = new ArrayList<String>();

      // Splits the string to each word and puts them into an array
      // '\\s' whitespace is the delimiter for the split method
      String[] splitedCommands = input.split(" ");
      for (String commandParam : splitedCommands) {
        commands.add(commandParam);
      }

      /*
       * This should be temporary until all of our commands are in its own class.
       */
      // Splits the string to each word and puts them into an array
      // '\\s' whitespace is the delimiter for the split method
      String[] inputSplit = input.split("\\s");

      if (commands.get(0).equalsIgnoreCase("help")) {
        // Command 2.1
        HelpCommand helpCommand = new HelpCommand();
        // Null because help does not need parameters
        System.out.println(helpCommand.help());
        continue;
      }
      else if (commands.get(0).equalsIgnoreCase("quit")) {
        // Command 2.2
        promptLoop = false;
        exitProgram();
      }
      else if (commands.get(0).equalsIgnoreCase("sources")) {
        // Command 2.3
        SourceListCommand listSources = new SourceListCommand();
        System.out.println(listSources.doCommand(null, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 2) && (commands.get(0).equalsIgnoreCase("summary"))) {
        // Command 2.4
        SummaryListCommand listSummary = new SummaryListCommand();
        System.out.println(listSummary.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 4) && (commands.get(0).equalsIgnoreCase("sensordata"))
          && (commands.get(2).equalsIgnoreCase("timestamp"))) {
        // Command 2.5
        SensorDataCommand sensorData = new SensorDataCommand();
        System.out.println(sensorData.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 4) && (commands.get(0).equalsIgnoreCase("dailysensordata"))
          && (commands.get(2).equalsIgnoreCase("day"))) {
        // Command 2.6
        SensorDataDayCommand sensorDataDay = new SensorDataDayCommand();
        System.out.println(sensorDataDay.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 5) && (commands.get(0).equalsIgnoreCase("power"))
          && (commands.get(3).equalsIgnoreCase("timestamp"))) {
        // Command 2.7
        PowerCommand listPower = new PowerCommand();
        System.out.println(listPower.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 9) && (commands.get(0).equalsIgnoreCase("powerstats"))
          && (commands.get(7).equalsIgnoreCase("statistic"))) {
        // Command 2.8
        PowerStatsCommand powerStats = new PowerStatsCommand();
        System.out.println(powerStats.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 8) && (commands.get(0).equalsIgnoreCase("total"))
          && (commands.get(2).equalsIgnoreCase("generated"))) {
        // Command 2.9
        TotalGenerated totalGenerated = new TotalGenerated();
        System.out.println(totalGenerated.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((commands.size() == 10) && (commands.get(0).equalsIgnoreCase("chart"))
          && (commands.get(8).equalsIgnoreCase("file"))) {
        // Command 2.10
        ChartPowerCommand chartPower = new ChartPowerCommand();
        System.out.println(chartPower.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((inputSplit.length == 2) && (inputSplit[0].equalsIgnoreCase("fueltypes"))) {
        // Command 2.11
        FuelTypesCommand fuelTypes = new FuelTypesCommand();
        System.out.println(fuelTypes.doCommand(commands, cli.getClient()));
      }
      else if ((inputSplit.length == 5) && (commands.get(0).equalsIgnoreCase("totalpower"))) {
        // Command 2.12
        TotalPowerCommand totalPower = new TotalPowerCommand();
        System.out.println(totalPower.doCommand(commands, cli.getClient()));
        continue;
      }
      else if ((inputSplit.length == 5) && (commands.get(0).equalsIgnoreCase("carboncontent"))) {
        // Command 2.13
        CarbonContentCommand carbonContent = new CarbonContentCommand();
        System.out.println(carbonContent.doCommand(commands, cli.getClient()));
        continue;
      }
      else {
        System.out.println("Please enter a correct command\nEnter help to see commands");
      }
    }
  }

  /**
   * Exits the program.
   */
  private void exitProgram() {
    System.out.println("Connection Terminated");
    System.exit(0);
  }

}
